package com.dimanche.library_base.utils;

import android.util.Log;

import com.hjq.toast.ToastUtils;

import java.util.Arrays;
import java.util.List;

/**
 * author : Dimanche
 * date   : 2020/7/3110:26
 * desc   : 排序工具类
 * version: 1.0
 */
public class SortUtils {
    private final static String TAG = "SortUtils";

    /**
     * 对集合中的内容进行升序排序(冒泡排序)
     * 用于对日志文件夹的排序
     *
     * @param oldList 待排序的集合
     * @return
     */
    public static List<String> sortFolderList(List<String> oldList) {
        if (oldList == null) {
            new Exception("待排序的集合为空");
        }
        try {
            for (int i = 0; i < oldList.size() - 1; i++) {
                for (int j = 0; j < oldList.size() - 1 - i; j++) {
                    String temp1 = oldList.get(j);
                    long time1 = TimeUtils.tranceLongYMDOfStr(temp1);
                    String temp2 = oldList.get(j + 1);
                    long time2 = TimeUtils.tranceLongYMDOfStr(temp2);
                    if (time1 > time2) {
                        oldList.set(j + 1, temp1);
                        oldList.set(j, temp2);
                    }
                }
            }
        } catch (Exception e) {
            e.getStackTrace();
            Log.e(TAG, "sortList: ", e);
            ToastUtils.show(e.getMessage());
        }
        return oldList;
    }

    /**
     * 对数组中的内容进行排序（冒泡排序）
     * 用于对日志文件夹的排序
     *
     * @param arr 待排序的数组
     * @return 排序完成的集合
     */
    public static List<String> sortFolderArr(String[] arr) {
        if (arr == null) {
            new Exception("待排序的集合为空");
        }
        List<String> oldList = Arrays.asList(arr);
        try {
            for (int i = 0; i < oldList.size() - 1; i++) {
                for (int j = 0; j < oldList.size() - 1 - i; j++) {
                    String temp1 = oldList.get(j);
                    long time1 = TimeUtils.tranceLongYMDOfStr(temp1);
                    String temp2 = oldList.get(j + 1);
                    long time2 = TimeUtils.tranceLongYMDOfStr(temp2);
                    if (time1 > time2) {
                        oldList.set(j + 1, temp1);
                        oldList.set(j, temp2);
                    }
                }
            }
        } catch (Exception e) {
            e.getStackTrace();
            Log.e(TAG, "sortList: ", e);
            ToastUtils.show(e.getMessage());
        }
        return oldList;
    }

}
